package com.pkk.spring.cloud.service.gateway.config;

import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.pkk.spring.cloud.config.nacos.factory.NacosConfigListenerFactory;
import com.pkk.spring.cloud.config.nacos.properties.NacosProperties;
import com.pkk.spring.cloud.config.nacos.properties.NacosProperties.NacosConfigProperties;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 网关的nacos的监听配置
 *
 * @author peikunkun
 * @version V1.0
 * @date 2021-01-27 13:36
 **/
@Slf4j
public class GatewayNacosConfigListener extends NacosConfigListenerFactory {


  @Autowired
  private NacosProperties nacosProperties;


  /**
   * 获取配置id
   *
   * @return java.lang.String
   * @Param configName
   * @author peikunkun
   * @date 2021/1/27 0027 下午 4:05
   * @since
   */
  public String getConfigId(@NonNull String configName) {
    final NacosConfigProperties nacosConfigProperties = nacosProperties.getDynamic().get(configName);
    return nacosConfigProperties.getServerAddr().concat("-").concat(nacosConfigProperties.getNamespace());
  }

  /**
   * 获取配置的子节点
   *
   * @return java.lang.String
   * @Param config
   * @Param nodeId
   * @author peikunkun
   * @date 2021/1/27 0027 下午 1:45
   * @since
   */
  public String getConfig(String config, String nodeId) throws NacosException {
    if (StringUtils.isBlank(config)) {
      return config;
    }
    return JSONUtil.parseObj(config).getStr(nodeId);
  }


  /**
   * 获取某一个节点的配置
   *
   * @return cn.hutool.json.JSONArray
   * @Param configService
   * @Param group
   * @Param dataId
   * @Param nodeId
   * @author peikunkun
   * @date 2021/1/27 0027 下午 1:47
   * @since
   */
  public String getConfig(ConfigService configService, String group, String dataId, String nodeId)
      throws NacosException {
    final String config = super.getConfig(configService, group, dataId);
    if (StringUtils.isBlank(config)) {
      return config;
    }
    return JSONUtil.parseObj(config).getStr(nodeId);
  }


  /**
   * 获取某一个节点的配置
   *
   * @return java.lang.String
   * @author peikunkun
   * @date 2021/1/27 0027 下午 3:35
   * @since
   */
  public String getConfigWithNode(@NonNull String configName, @NonNull String nodeId) throws NacosException {
    final NacosConfigProperties nacosConfigProperties = nacosProperties.getDynamic().get(configName);
    if (null == nacosConfigProperties) {
      log.warn("[Gateway] Configuration file that does not exist -[{}]-[{}]", configName, nodeId);
      return null;
    }

    //配置项id
    final String configId = this
        .getConfigId(nacosConfigProperties.getServerAddr(), nacosConfigProperties.getNamespace());
    if (StringUtils.isBlank(configId)) {
      log.warn("[Gateway]-Gateway configuration id that does not exist -[{}]-[{}]", configName, nodeId);
      return null;
    }

    ConfigService configService = this.getConfig(configId);
    if (null == configService) {
      log.warn("[Gateway]-Gateway configuration  that does not exist -[{}]-[{}]", configName, nodeId);
      return null;
    }
    String configInfo = this
        .getConfig(configService, nacosConfigProperties.getGroup(), nacosConfigProperties.getDataId(), nodeId);
    if (StringUtils.isBlank(configInfo)) {
      log.warn("Gateway routing information not obtained, please check the service! -[{}]-[{}]", configName, nodeId);
    }
    return configInfo;
  }

  /**
   * 获取配置名称
   *
   * @return com.pkk.spring.cloud.config.nacos.properties.NacosProperties.NacosConfigProperties
   * @Param configName
   * @author peikunkun
   * @date 2021/1/27 0027 下午 5:17
   * @since
   */
  public NacosConfigProperties getConfigWithConfigName(@NonNull String configName) {
    return nacosProperties.getDynamic().get(configName);
  }


}
